<template>
    <div>
        <mixinsObj></mixinsObj>
        <el-card>
            <el-row :gutter="20">
                <el-col :span="8">
                    <el-input placeholder="请输入内容" class="input-with-select" clearable>
                        <el-button slot="append" icon="el-icon-search"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="3">
                    <el-button type="primary" @click="addGoods">添加用户</el-button>
                </el-col>
            </el-row>
            <el-table :data="tableData" border style="width: 100%">
                <el-table-column type="index" label="#">
                </el-table-column>
                <el-table-column prop="goods_name" label="商品名称" width="400px">
                </el-table-column>
                <el-table-column prop="goods_price" label="商品价格">
                </el-table-column>
                <el-table-column prop="goods_weight" label="商品重量">
                </el-table-column>
                <el-table-column label="创建时间">
                    <template slot-scope="scope">
                        {{ parseTime(scope.row.upd_time) }}
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-edit" circle size="mini"
                            @click="edit(scope.row.goods_id)"></el-button>
                        <el-button type="danger" icon="el-icon-delete" circle size="mini"
                            @click="del(scope.row.goods_id)">
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>

            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page="query.pagenum" :page-sizes="[3, 5, 8, 10]" :page-size="query.pagesize"
                layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </el-card>
    </div>
</template>
<script>
import mixinsObj from '@/views/mixins.vue'
import { getList, delGoods } from '@/utils/Addgoods'
export default {
    components: { mixinsObj },
    data() {
        return {
            tableData: [],
            query: {
                query: '',
                pagenum: 1,
                pagesize: 5
            },
            total: 0,
            type: 'add'
        }
    },
    methods: {
        addGoods() {
            let a = this.$router.getRoutes()
            let index = a.findIndex(item => {
                return item.path == '/addgoods'
            })
            if (index == -1) {
                let obj = {
                    path: '/addgoods',
                    name: 'Addgoods',
                    component: () => import('@/components/Addgoods.vue')
                }
                this.$router.addRoute('Home', obj)
                this.$store.commit('routeModule/addRouter', obj)
            }
            this.$router.push('/addgoods?type=add')
        },
        async getList() {
            let { data: res } = await getList(this.query)
            this.tableData = res.data.goods
            this.total = res.data.total
        },
        parseTime(time, cFormat) {
            if (arguments.length === 0) {
                return null;
            }
            const format = cFormat || "{y}-{m}-{d} {h}:{i}:{s}";
            let date;
            if (typeof time === "object") {
                date = time;
            } else {
                if (typeof time === "string" && /^[0-9]+$/.test(time)) {
                    time = parseInt(time);
                }
                if (typeof time === "number" && time.toString().length === 10) {
                    time = time * 1000;
                }
                date = new Date(time);
            }
            const formatObj = {
                y: date.getFullYear(),
                m: date.getMonth() + 1,
                d: date.getDate(),
                h: date.getHours(),
                i: date.getMinutes(),
                s: date.getSeconds(),
                a: date.getDay(),
            };
            const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
                const value = formatObj[key];
                // Note: getDay() returns 0 on Sunday
                if (key === "a") {
                    return ["日", "一", "二", "三", "四", "五", "六"][value];
                }
                return value.toString().padStart(2, "0");
            });
            return time_str;
        },
        handleSizeChange(val) {
            this.query.pagesize = val
            this.getList()
        },
        handleCurrentChange(val) {
            this.query.pagenum = val
            this.getList()
        },
        del(id) {

            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {

                let { data: res } = await delGoods(id)
                if (res.meta.status == 200) {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getList()
                }
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        edit(id) {
            let a = this.$router.getRoutes()
            let index = a.findIndex(item => {
                return item.path == '/addgoods'
            })
            if (index == -1) {
                let obj = {
                    path: '/addgoods',
                    name: 'Addgoods',
                    component: () => import('@/components/Addgoods.vue')
                }
                this.$router.addRoute('Home', obj)
                this.$store.commit('routeModule/addRouter', obj)
            }
            this.$router.push(`/addgoods?id=${id}&&type=edit`)
        }

    },
    mounted() {
        this.getList()
    }
}
</script>